package com.yj.rselasticsearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yj.common.constant.DataConstants;
import com.yj.common.constant.DataScopeConstants;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.*;
import com.yj.common.core.domain.model.LoginUser;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.SecurityUtils;
import com.yj.common.utils.StringUtils;
import com.yj.rselasticsearch.domain.dto.TagInfoDto;
import com.yj.rselasticsearch.service.FileTypeService;
import com.yj.system.domain.SysRoleDept;
import com.yj.system.service.ISysDeptService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.yj.rselasticsearch.mapper.TagInfoMapper;
import com.yj.rselasticsearch.service.TagInfoService;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagInfoServiceImpl extends ServiceImpl<TagInfoMapper, TagInfo> implements TagInfoService {
    @Resource
    private TagInfoMapper tagInfoMapper;

    @Resource
    private FileTypeService fileTypeService;

    @Resource
    private ISysDeptService sysDeptService;

    @Override
    public AjaxResult deleteTagInfoById(Integer id) {
        TagInfo tagInfo = tagInfoMapper.selectById(id);
        tagInfo.setStatus(DataConstants.Number.ONE);
        tagInfo.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(tagInfoMapper.updateById(tagInfo));
    }

    @Override
    public AjaxResult getAllList() {
        //获取用户数据权限信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRoleDept> sysRoleDepts = fileTypeService.getRoleDepts(null).getSysRoleDepts();
        List<SysRoleFileType> roleFileTypes =fileTypeService.getRoleDepts(null).getFileTypes();
        //查询文件类型
        //获取标签列表
        List<TagInfo> tagInfoList =list(new LambdaQueryWrapper<TagInfo>().
                eq(TagInfo::getStatus, DataConstants.Number.ZERO));
        //数据过滤
        List<TagInfo> tagInfos = new ArrayList<>();
        String dataScope = null;
        for (SysRoleDept sysRoleDept : sysRoleDepts) {
            if (StringUtils.isEmpty(sysRoleDept.getDataScope())) {
                continue;
            }else{
                dataScope = sysRoleDept.getDataScope();
            }
        }
        if (!FastUtils.checkNullOrEmpty(sysRoleDepts)&&!DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope)){
            for (TagInfo tagInfo : tagInfoList) {
                for (SysRoleDept sysRoleDept : sysRoleDepts) {
                    if (tagInfo.getDeptId().equals(sysRoleDept.getDeptId())) {
                        tagInfos.add(tagInfo);
                        break;
                    }
                }
            }
        }else if (DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope)){
            for (TagInfo tagInfo : tagInfoList) {
                if (tagInfo.getCreateBy().equals(loginUser.getUsername())){
                    tagInfos.add(tagInfo);
                }
            }
        }
        else{
            tagInfos = tagInfoList;
        }
        return AjaxResult.success(tagInfos);
    }

    /**
     * 分页查询标签信息
     * @return
     */
    @Override
    public AjaxResult getAllListByPage(TagInfoDto tagInfoDto) {
        IPage<TagInfo> tagInfoIPage = new Page<>(tagInfoDto.getPageIndex(), tagInfoDto.getPageSize());
        //获取用户数据权限信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRoleDept> sysRoleDepts = fileTypeService.getRoleDepts(null).getSysRoleDepts();
        List<SysRoleFileType> roleFileTypes =fileTypeService.getRoleDepts(null).getFileTypes();
        //查询文件类型
        //获取标签列表
        List<TagInfo> tagInfoList =list( new LambdaQueryWrapper<TagInfo>().
                eq(TagInfo::getStatus, DataConstants.Number.ZERO).
                eq(tagInfoDto.getDeptId()!=null,TagInfo::getDeptId,tagInfoDto.getDeptId()).orderByDesc(TagInfo::getCreateTime));
        //数据过滤
        List<TagInfo> tagInfos = new ArrayList<>();
        String dataScope = null;
        for (SysRoleDept sysRoleDept : sysRoleDepts) {
            if (StringUtils.isEmpty(sysRoleDept.getDataScope())) {
                continue;
            }else{
                dataScope = sysRoleDept.getDataScope();
            }
        }
        if (!FastUtils.checkNullOrEmpty(sysRoleDepts)&&!DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope)){
            for (TagInfo tagInfo : tagInfoList) {
                for (SysRoleDept sysRoleDept : sysRoleDepts) {
                    if (tagInfo.getDeptId().equals(sysRoleDept.getDeptId())) {
                        tagInfos.add(tagInfo);
                    }
                }
            }
        }else if (DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope)){
            for (TagInfo tagInfo : tagInfoList) {
                if (tagInfo.getCreateBy().equals(loginUser.getUsername())){
                    tagInfos.add(tagInfo);
                }
            }
        }
        else{
            tagInfos = tagInfoList;
        }
        List<SysDept> sysDeptList = sysDeptService.selectDeptListVo();
        tagInfos.forEach(data->{
            sysDeptList.forEach(dept->{
                if (String.valueOf(data.getDeptId()).equals(String.valueOf(dept.getDeptId()))){
                    data.setDeptName(dept.getDeptName());
                }
            });
        });
        //分页信息
        tagInfoIPage.setTotal(tagInfos.size());

        tagInfoIPage.setCurrent(tagInfoDto.getPageIndex());
        tagInfoIPage.setSize(tagInfoDto.getPageSize());
        tagInfoIPage.setPages(tagInfos.size() % tagInfoDto.getPageSize());
        int fromIndex = ((int) tagInfoIPage.getCurrent()-1)*(int)tagInfoIPage.getSize(); //第几行数据开始
        int toIndex = fromIndex + (int)tagInfoIPage.getSize(); //第几行结束
        if (toIndex > (int)tagInfoIPage.getTotal()){
            toIndex = (int)tagInfoIPage.getTotal();
        }
        List<TagInfo> pageList = tagInfos.subList(fromIndex,toIndex);
        tagInfoIPage.setRecords(pageList);
        return AjaxResult.success(tagInfoIPage);
    }
}
